//
//  EditHeightViewController.m
//  WeightMaster
//
//  Created by maoyu on 15/8/18.
//  Copyright (c) 2015年 buddysoft. All rights reserved.
//

#import "EditHeightViewController.h"
#import "BDFoundation.h"
#import "WMAppSetting.h"

#define MIN_HEIGHT  150
#define MAX_HEIGHT  230
#define MALE_HEIGHT 175
#define FEMALE_HEIGHT   160

@interface EditHeightViewController () {
    NSMutableArray * _heights;
}

@end

@implementation EditHeightViewController

#pragma 私有函数
- (void)initView {
    [self.nextButton makeCornorRadius:17.0];
    
    self.pickerView.showsSelectionIndicator = YES;
}

#pragma 事件函数
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"输入您的身高(厘米)";
    
    [self initView];
    
    _heights = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger index = MIN_HEIGHT; index <= MAX_HEIGHT; index++) {
        [_heights addObject:[NSNumber numberWithInteger:index]];
    }
    
    if ([WMAppSetting sharedInstance].height && MIN_HEIGHT <= [[WMAppSetting sharedInstance].height integerValue]) {
        [_pickerView selectRow:[[WMAppSetting sharedInstance].height integerValue]  - MIN_HEIGHT inComponent:0 animated:NO];
    }else {
        if (WMGenderMale == [[WMAppSetting sharedInstance].gender integerValue]) {
            [_pickerView selectRow:MALE_HEIGHT - MIN_HEIGHT inComponent:0 animated:NO];
        }else {
            [_pickerView selectRow:FEMALE_HEIGHT - MIN_HEIGHT inComponent:0 animated:NO];
        }
    }
    
    if (self.editMode) {
        [self.nextButton setTitle:@"保存" forState:UIControlStateNormal];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)next:(id)sender {
    NSInteger row =[self.pickerView selectedRowInComponent:0];
    NSNumber * height = [_heights objectAtIndex:row];
    [WMAppSetting sharedInstance].height = height;
    
    if (self.editMode) {
        [[WMAppSetting sharedInstance] syncData];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self performSegueWithIdentifier:@"ToEditWeight" sender:self];        
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 90.0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel * label = view ? (UILabel *) view : [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kScreen_Width, 90.0f)];
    
    NSString * height = [[_heights objectAtIndex:row] stringValue];
    label.text = height;
    label.textAlignment = NSTextAlignmentCenter;
    [label setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:50.0]];
  
    return label;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [_heights count];
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [[_heights objectAtIndex:row] stringValue];
}
@end
